$(function() {

	//获取登陆用户session
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/user/getSession",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var code = data.code;
			if (code == 'success') {
				window.location.href = './index.html';
			}
		}
	});

	//登陆
	$("#sub_form").click(function() {
		var username = $('#username').val();
		var password = $('#password').val();
		if (username == '') {
			layer.tips('请输入用户名', '#username');
		}
		if (password == '') {
			layer.tips('请输入密码', '#password', {
				tipsMore: true,
			});
		}
		if (username == '' || password == '') {
			return;
		}
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/user/login",
			dataType: "json",
			xhrFields: {
				withCredentials: true
			},
			data: {
				"userName": username,
				"password": password
			},
			success: function(data) {
				var code = data.code;
				if (code == 'success') { //如果登录成功，跳转到登录界面
					window.location.href = './index.html';
				} else {
					layer.msg(data.msg);
				}
			}
		});
	});
});
